/**
 * Created by chenzui on 2017/3/29.
 */
var code;
var shengxiao;
var leftCode;
var wuxing;
var bose;
function commit() {
    var codeBoxes = $("input[name=rad1]");
    var shengxiaoBoxes =  $("input[name=rad2]");
    var leftCodeBoxes =  $("input[name=rad3]");
    var wuxingBoxes =  $("input[name=rad4]");
    var boseBoxes =  $("input[name=rad5]");
    var param={};
    $.each(codeBoxes,function (i,v) {
        if(v.checked == true){
            param.code = v.value;
        }
    })

    $.each(shengxiaoBoxes,function (i,v) {
        if(v.checked == true){
            param.shengxiao = v.value;
        }
    })

    $.each(leftCodeBoxes,function (i,v) {
        if(v.checked == true){
            param.leftCode = v.value;
        }
    })

    var wuxingVal="";
    $.each(wuxingBoxes,function (i,v) {
        if(v.checked == true){
            wuxingVal += v.value+","
        }
    })
    param.wuxing = wuxingVal.substr(0,wuxingVal.length-1);

    var boseVal = "";
    $.each(boseBoxes,function (i,v) {
        if(v.checked == true){
            boseVal += v.value+","
        }
    })
    param.bose = boseVal.substr(0,boseVal.length-1);

    Ajax.ajax({
        url: "/lottery/add",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                alert("预测发布成功");
            } else {
                alert("预测发布失败");
            }
            console.log(res);
        },
        error: function(){
            alert("操作失败");
        }
    });

}
function getPredict() {
    Ajax.ajax({
        url: "/lottery/getPredict",
        data: {},
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                code = res.data.code;
                shengxiao = res.data.shengxiao;
                leftCode=res.data.left_code;
                wuxing = res.data.wuxing.split(",");
                bose = res.data.bose.split(",");
                initPage();
            } else {

            }
            console.log(res);
        },
        error: function(){
            alert("操作失败");
        }
    });
}
function initPage() {
   debugger;
    var codeBoxes = $("input[name=rad1]");
    var shengxiaoBoxes =  $("input[name=rad2]");
    var leftCodeBoxes =  $("input[name=rad3]");
    var wuxingBoxes =  $("input[name=rad4]");
    var boseBoxes =  $("input[name=rad5]");
    
    $.each(codeBoxes,function (i,v) {
        if(code == v.value){
            v.checked = true;
        }
    })

    $.each(shengxiaoBoxes,function (i,v) {
        if(shengxiao == v.value){
            v.checked = true;
        }
    })

    $.each(leftCodeBoxes,function (i,v) {
        if(leftCode == v.value){
            v.checked = true;
        }
    })

    $.each(wuxingBoxes,function (i,v) {
        $.each(wuxing,function (j,jv) {
            if(jv == v.value){
                v.checked = true;
            }
        })
    })

    $.each(boseBoxes,function (i,v) {
        $.each(bose,function (j,jv) {
            if(jv == v.value){
                v.checked = true;
            }
        })
    })

}
$(function () {
    getPredict();
    $('#predictCommitBtn').on('click',function () {
        commit();
    });
})